/*
-------------------------------------------------

	Input.cpp

	Dmitriy Kuzmenko (Dgut)

-------------------------------------------------
*/

#include "Input.h"
#include "Common.h"

#if defined WIN32
#include <windows.h>

extern bool g_active;
#endif

namespace Autumn
{

Input::Input( void ) :
	numPrevTouches	( 0 ),
	numTouches		( 0 )
{
}

Input::~Input( void )
{
}

const bool Input::Init( void )
{
	memset( keys, 0, sizeof( keys ) );

	return true;
}

void Input::Shutdown( void )
{
}

void Input::Frame( void )
{
#if defined WIN32
	if( g_active )
	{
/*		if( GetKeyState( '1' ) & 0x8000 )
			gRender.SetTheme( pRainbow );
		if( GetKeyState( '2' ) & 0x8000 )
			gRender.SetTheme( pNight );*/
/*		left = left || ( GetKeyState( 'A' ) & 0x8000 );
		right = right || ( GetKeyState( 'D' ) & 0x8000 );
		jump = jump || ( GetKeyState( ' ' ) & 0x8000 ) || ( GetKeyState( 'W' ) & 0x8000 );

		if( GetKeyState( 'Q' ) & 0x8000 )
			buttonLayout = ( ButtonLayout )( ( buttonLayout + 1 ) % 3 );*/
	}
#endif

	// using new touches...
}

void Input::Save( void )
{
	// save old state
	memcpy( prevTouches, touches, sizeof( touches ) );
	memcpy( prevKeys, keys, sizeof( keys ) );

	numPrevTouches = numTouches;
}

void Input::Touch( const float * points, int numPoints )
{
	if( numPoints > MAX_TOUCHES )
		numPoints = MAX_TOUCHES;
	
	for( int i = 0; i < numPoints * 2; i++ )
		touches[ i ] = points[ i ];
	
	numTouches = numPoints;
}

void Input::Key( int key, bool down )
{
	keys[ key ] = down;
}

const float * const Input::GetTouches( void ) const
{
	return touches;
}

const int Input::GetNumTouches( void ) const
{
	return numTouches;
}

const bool Input::GetKey( int key ) const
{
	return keys[ key ];
}

const bool Input::IsDown( float x, float y, float width, float height ) const
{
	if( !numTouches || numPrevTouches )
		return false;
	
	for( int i = 0; i < numTouches; i++ )
		if( touches[ i * 2 + 0 ] < x ||
			touches[ i * 2 + 1 ] < y ||
			touches[ i * 2 + 0 ] > x + width ||
			touches[ i * 2 + 1 ] > y + height )
			return false;
	
	return true;
}
	
const bool Input::IsMove( float x, float y, float width, float height ) const
{
	if( !numTouches )
		return false;
	
	for( int i = 0; i < numTouches; i++ )
		if( touches[ i * 2 + 0 ] > x &&
			touches[ i * 2 + 1 ] > y &&
			touches[ i * 2 + 0 ] < x + width &&
			touches[ i * 2 + 1 ] < y + height )
			return true;
	
	return false;
}

const bool Input::IsEnter( float x, float y, float width, float height ) const
{
	if( !numTouches )
		return false;

	bool can = false;

	for( int i = 0; i < numTouches; i++ )
		if( touches[ i * 2 + 0 ] > x &&
			touches[ i * 2 + 1 ] > y &&
			touches[ i * 2 + 0 ] < x + width &&
			touches[ i * 2 + 1 ] < y + height )
		{
			can = true;
			break;
		}

	if( !can )
		return false;

	for( int i = 0; i < numPrevTouches; i++ )
		if( prevTouches[ i * 2 + 0 ] > x &&
			prevTouches[ i * 2 + 1 ] > y &&
			prevTouches[ i * 2 + 0 ] < x + width &&
			prevTouches[ i * 2 + 1 ] < y + height )
		return false;

	return true;
}

const bool Input::IsUp( float x, float y, float width, float height ) const
{
	if( numTouches || !numPrevTouches )
		return false;
	
	for( int i = 0; i < numPrevTouches; i++ )
		if( prevTouches[ i * 2 + 0 ] < x ||
			prevTouches[ i * 2 + 1 ] < y ||
			prevTouches[ i * 2 + 0 ] > x + width ||
			prevTouches[ i * 2 + 1 ] > y + height )
			return false;
	
	return true;
}

const bool Input::IsDown( void ) const
{
	return numTouches == 1 && numPrevTouches != 1;
}

const bool Input::IsUp( void ) const
{
	return numTouches != 1 && numPrevTouches == 1;
}

const bool Input::IsMove( void ) const
{
	return numTouches == 1 && numPrevTouches == 1;
}

const bool Input::IsKeyDown( int key ) const
{
	return keys[ key ] && !prevKeys[ key ];
}

const bool Input::IsKeyUp( int key ) const
{
	return !keys[ key ] && prevKeys[ key ];
}

}	// namespace Autumn